﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TSharp.Core.Util
{
    /// <summary>
    /// 
    /// </summary>
    /// <author>
    /// tangjingbo
    /// </author>
    /// <remarks>
    /// tangj15 at 2012-5-4 11:58
    /// </remarks>
    public static class Helper
    {
        /// <summary>
        /// Tries the specified unsafe code.
        /// </summary>
        /// <param name="unsafeCode">The unsafe code.</param>
        /// <param name="catchCode">The catch code.</param>
        /// <param name="finallyCode">The finally code.</param>
        public static void Try(Action unsafeCode, Action<Exception> catchCode, Action finallyCode)
        {
            try
            {
                if (unsafeCode != null) unsafeCode();
            }
            catch (Exception ex)
            {
                if (catchCode != null) catchCode(ex);
            }
            finally
            {
                if (finallyCode != null) finallyCode();
            }
        }

        /// <summary>
        /// Tries the specified unsafe code.
        /// </summary>
        /// <typeparam name="TException">The type of the exception.</typeparam>
        /// <param name="unsafeCode">The unsafe code.</param>
        /// <param name="catchCode">The catch code.</param>
        /// <param name="finallyCode">The finally code.</param>
        public static void Try<TException>(Action unsafeCode, Action<TException> catchCode, Action finallyCode)
            where TException : Exception
        {
            try
            {
                if (unsafeCode != null) unsafeCode();
            }
            catch (TException ex)
            {
                if (catchCode != null) catchCode(ex);
            }
            finally
            {
                if (finallyCode != null) finallyCode();
            }
        }
    }
}